//
//  DHSale_FilterView.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/12.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSale_FilterView.h"
#import "ColorItem.h"
#import "S2_LabelItem.h"
#import "CategoryItem.h"

@interface DHSale_FilterView()

@property (weak, nonatomic) IBOutlet UIScrollView *colorScrollView;
@property (weak, nonatomic) IBOutlet UIScrollView *typeScrollView;
@property (weak, nonatomic) IBOutlet UIScrollView *categoryScrollView;
@property (weak, nonatomic) IBOutlet UIButton *manButton;
@property (weak, nonatomic) IBOutlet UIButton *womanButton;

@property (strong, nonatomic) NSArray *colorArr;
@property (strong, nonatomic) NSArray *labelArr;
@property (strong, nonatomic) NSMutableArray *categoryArr;

@end

@implementation DHSale_FilterView

+ (DHSale_FilterView *)shareDHSale_FilterView{
    DHSale_FilterView *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DHSale_FilterView class]) owner:self options:nil] objectAtIndex:0];
    return view;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    [self.manButton bk_addEventHandler:^(id sender) {
        [self.manButton setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
        [self.womanButton setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
        
        NSMutableDictionary *filter_dic = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER]];
        [filter_dic setObject:@"男" forKey:@"sex"];
        [filter_dic setObject:@"" forKey:@"category_id"];
        [[NSUserDefaults standardUserDefaults] setObject:filter_dic forKey:USER_SALE_FILTER];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        [self loadCategoryDataWithSex:@"男"];
        
        [self routerEventWithName:@"filter_update" userInfo:nil];
        
    } forControlEvents:UIControlEventTouchUpInside];
    [self.womanButton bk_addEventHandler:^(id sender) {
        [self.womanButton setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
        [self.manButton setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
        
        NSMutableDictionary *filter_dic = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER]];
        [filter_dic setObject:@"女" forKey:@"sex"];
        [filter_dic setObject:@"" forKey:@"category_id"];
        [[NSUserDefaults standardUserDefaults] setObject:filter_dic forKey:USER_SALE_FILTER];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        [self loadCategoryDataWithSex:@"女"];
        
        [self routerEventWithName:@"filter_update" userInfo:nil];
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    NSDictionary *filter_dic = [[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER];
    if ([[filter_dic objectForKey:@"sex"] length] > 0) {
        if ([[filter_dic objectForKey:@"sex"] isEqualToString:@"男"]) {
            [self.womanButton setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
            [self.manButton setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
        }else{
            [self.womanButton setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
            [self.manButton setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
        }
    }
}

- (void)loadDHSale_FilterViewData{
    [[DHNetAPIClient shareDHNetAPIClient] request_colorsAndBlock:^(id data, NSError *error) {
        self.colorArr = [data objectForKey:@"colors"];
        [self.colorScrollView removeAllSubviews];
        self.colorScrollView.contentSize = CGSizeMake(self.colorScrollView.height * self.colorArr.count, self.colorScrollView.height);
        
        NSDictionary *filter_dic = [[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER];
        NSString *filter_color = [filter_dic objectForKey:@"color"];
        
        for (int i = 0; i < self.colorArr.count; i++) {
            NSDictionary *dic = [self.colorArr objectAtIndex:i];
            ColorItem *colorItem = [[ColorItem alloc] initWithFrame:CGRectMake(self.colorScrollView.height * i, 0, self.colorScrollView.height, self.colorScrollView.height) color:[UIColor colorWithHexString:[dic objectForKey:@"color"]]];
            colorItem.tag = i;
            [colorItem addTarget:self action:@selector(colorItemAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.colorScrollView addSubview:colorItem];
            if ([[dic objectForKey:@"name"] isEqualToString:filter_color]) {
                [self colorItemAction:colorItem];
            }
        }
    }];
    
    [[DHNetAPIClient shareDHNetAPIClient] request_labelsWithParams:@{@"hot":@"1"} andBlock:^(id data, NSError *error) {
        self.labelArr = [data objectForKey:@"labels"];
        [self.typeScrollView removeAllSubviews];
        
        NSDictionary *filter_dic = [[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER];
        NSString *filter_label_id = [filter_dic objectForKey:@"label_id"];
        
        CGFloat spaceX = 5;
        for (int i = 0; i < self.labelArr.count; i++) {
            NSDictionary *dic = [self.labelArr objectAtIndex:i];
            NSString *title = [dic objectForKey:@"title"];
            CGFloat width = [title widthForFont:[UIFont systemFontOfSize:15]];
            if (width < 50) {
                width = 50;
            }
            width += 18;
            S2_LabelItem *item = [[S2_LabelItem alloc] initWithFrame:CGRectMake(spaceX, 5, width, self.typeScrollView.height - 10) titleStr:title];
            item.tag = i;
            [item addTarget:self action:@selector(labelItemAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.typeScrollView addSubview:item];
            spaceX += width;
            spaceX += 5;
            
            if ([[dic objectForKey:@"_id"] isEqualToString:filter_label_id]) {
                [self labelItemAction:item];
            }
        }
        self.typeScrollView.contentSize = CGSizeMake(spaceX, self.typeScrollView.height);
    }];
    
    NSDictionary *filter_dic = [[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER];
    if ([[filter_dic objectForKey:@"sex"] length] > 0) {
        if ([[filter_dic objectForKey:@"sex"] isEqualToString:@"男"]) {
            [self loadCategoryDataWithSex:@"男"];
        }else{
            [self loadCategoryDataWithSex:@"女"];
        }
    }else{
        [self loadCategoryDataWithSex:@"男"];
    };
    
}

- (void)loadCategoryDataWithSex:(NSString *)sex{
    [[DHNetAPIClient shareDHNetAPIClient] request_categoriesWithParams:@{@"sex":sex} andBlock:^(id data, NSError *error) {
        [self.categoryArr removeAllObjects];
        [self.categoryArr addObjectsFromArray:[data objectForKey:@"categories"]];
        
        [self.categoryScrollView removeAllSubviews];
        
        NSDictionary *filter_dic = [[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER];
        NSString *filter_category_id = [filter_dic objectForKey:@"category_id"];
        
        CGFloat spaceX = 5;
        for (int i = 0; i < self.categoryArr.count; i++) {
            NSDictionary *dic = [self.categoryArr objectAtIndex:i];
            NSString *title = [dic objectForKey:@"title"];
            CGFloat width = [title widthForFont:[UIFont systemFontOfSize:15]] + 5;
            if (width < 50) {
                width = 50;
            }
            width += 40;
            CategoryItem *item = [[CategoryItem alloc] initWithFrame:CGRectMake(spaceX, 5, width, self.typeScrollView.height - 10) title:title icon:[dic objectForKey:@"icon"]];
            item.tag = i;
            [item addTarget:self action:@selector(categoryItemAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.categoryScrollView addSubview:item];
            spaceX += width;
            spaceX += 5;
            
            if ([[dic objectForKey:@"_id"] isEqualToString:filter_category_id]) {
                [self categoryItemAction:item];
            }
        }
        self.categoryScrollView.contentSize = CGSizeMake(spaceX, self.categoryScrollView.height);
    }];
}

- (void)colorItemAction:(ColorItem *)colorItem{
    
    if (colorItem.isLarge) {
        [colorItem narrow];
        colorItem.isLarge = NO;
        
        NSDictionary *dic = [self.colorArr objectAtIndex:colorItem.tag];
        NSMutableDictionary *filter_dic = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER]];
        [filter_dic setObject:@"" forKey:@"color"];
        [[NSUserDefaults standardUserDefaults] setObject:filter_dic forKey:USER_SALE_FILTER];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self routerEventWithName:@"filter_update" userInfo:nil];
        
    }else{
        [colorItem enlarge];
        colorItem.isLarge = YES;
        
        NSDictionary *dic = [self.colorArr objectAtIndex:colorItem.tag];
        NSMutableDictionary *filter_dic = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER]];
        [filter_dic setObject:[dic objectForKey:@"name"] forKey:@"color"];
        [[NSUserDefaults standardUserDefaults] setObject:filter_dic forKey:USER_SALE_FILTER];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self routerEventWithName:@"filter_update" userInfo:nil];
    }
    for (ColorItem *item in self.colorScrollView.subviews) {
        if (item.tag != colorItem.tag) {
            item.isLarge = NO;
            [item narrow];
        }
    }
}

- (void)labelItemAction:(S2_LabelItem *)labelItem{
    if (labelItem.isLarge) {
        [labelItem narrow];
        labelItem.isLarge = NO;
        
        NSMutableDictionary *filter_dic = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER]];
        [filter_dic setObject:@"" forKey:@"label_id"];
        [[NSUserDefaults standardUserDefaults] setObject:filter_dic forKey:USER_SALE_FILTER];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self routerEventWithName:@"filter_update" userInfo:nil];
    }else{
        [labelItem enlarge];
        labelItem.isLarge = YES;
        
        NSDictionary *dic = [self.labelArr objectAtIndex:labelItem.tag];
        NSMutableDictionary *filter_dic = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER]];
        [filter_dic setObject:[dic objectForKey:@"_id"] forKey:@"label_id"];
        [[NSUserDefaults standardUserDefaults] setObject:filter_dic forKey:USER_SALE_FILTER];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self routerEventWithName:@"filter_update" userInfo:nil];
    }
    for (S2_LabelItem *item in self.typeScrollView.subviews) {
        if (item.tag != labelItem.tag) {
            item.isLarge = NO;
            [item narrow];
        }
    }
}

- (void)categoryItemAction:(CategoryItem *)categoryItem{
    if (categoryItem.isLarge) {
        [categoryItem narrow];
        categoryItem.isLarge = NO;
        
        NSDictionary *dic = [self.categoryArr objectAtIndex:categoryItem.tag];
        NSMutableDictionary *filter_dic = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER]];
        [filter_dic setObject:@"" forKey:@"category_id"];
        [[NSUserDefaults standardUserDefaults] setObject:filter_dic forKey:USER_SALE_FILTER];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self routerEventWithName:@"filter_update" userInfo:nil];
    }else{
        [categoryItem enlarge];
        categoryItem.isLarge = YES;
        
        NSDictionary *dic = [self.categoryArr objectAtIndex:categoryItem.tag];
        NSMutableDictionary *filter_dic = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:USER_SALE_FILTER]];
        [filter_dic setObject:[dic objectForKey:@"_id"] forKey:@"category_id"];
        [[NSUserDefaults standardUserDefaults] setObject:filter_dic forKey:USER_SALE_FILTER];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self routerEventWithName:@"filter_update" userInfo:nil];
    }
    for (CategoryItem *item in self.categoryScrollView.subviews) {
        if (item.tag != categoryItem.tag) {
            item.isLarge = NO;
            [item narrow];
        }
    }
}

- (IBAction)bgAction:(id)sender {
    [self routerEventWithName:@"hide_filter_view" userInfo:nil];
}

#pragma mark - arr
- (NSArray *)colorArr{
    if (!_colorArr) {
        _colorArr = [NSArray array];
    }
    return _colorArr;
}

- (NSArray *)labelArr{
    if (!_labelArr) {
        _labelArr = [NSArray array];
    }
    return _labelArr;
}

- (NSMutableArray *)categoryArr{
    if (!_categoryArr) {
        _categoryArr = [NSMutableArray array];
    }
    return _categoryArr;
}

@end
